<?php

require_once 'Mobitv/Admin/Action.php';

class SearchAction extends Mobitv_Admin_Action {

    public function execute() {
        // Neu chi la CP, khong phai CM hay Admin thi chi duoc xem myvideo
        if (isset($_SESSION['viewall']) && !$_SESSION['viewall'])
            $this->redirect('/myvideo');
        //$this->checkPermission();
        $this->updateLoginStatus();
        $videoDao = Vega_Dao::factory('Mobitv_Admin_Video');
        $limit = 10;
        $videoList = $featuredVideos = array();

        $page = isset($_GET['page']) && $_GET['page'] > 0 ? intval($_GET['page']) : 1;
        $searchType = isset($_GET['search_type']) ? $_GET['search_type'] : 'name';
        $keyword = isset($_GET['keyword']) ? $_GET['keyword'] : '';
        $keyword = strip_tags($keyword);

        if ($keyword) {
            if ($searchType == 'id') {
                $video = $videoDao->getDetail($keyword);
                if ($video)
                    $videoList[] = $video;
                $totalRow = count($videoList);
            }
            else {
                $videoList = $videoDao->search($keyword, $page, $limit);
                $totalRow = $videoDao->countAllBySearch($keyword);
            }

            $featuredVideos = $videoDao->getIdListFeaturedVideo();
            $this->_view->assign('featuredVideos', $featuredVideos);
        }

        $totalPage = ceil($totalRow / $limit);
        $this->_view->assign('keyword', $keyword);
        $this->_view->assign('curPage', $page);
        $this->_view->assign('totalRow', $totalRow);
        $this->_view->assign('totalPage', $totalPage);
        $this->_view->assign('videoList', $videoList);
    }

}

